const datagram = require("dgram");
const clientSocket = datagram.createSocket("udp4");

const getIpAddress = require("../ip");

const ip = getIpAddress();
const port = 15488
console.log("ip=", ip, 'port',port);


clientSocket.on('error', e => {
  console.log('error e=', e)
  searcher.close()
  sender.close()
})

clientSocket.on('message', async msg => {
  const text = msg.toString()
  console.log('message=', text)
  const obj = JSON.parse(text)
  const server_ip = obj.ip
  const server_port = obj.port

  const res = {ip, port, msg: 'aaa'}
  const resStr = JSON.stringify(res)

  clientSocket.send(resStr, server_port, server_ip)
  setTimeout(() => {
    clientSocket.close()
  }, 1000);
})


clientSocket.bind(port, ip)
